package com.woniu108.car.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.car.param.*;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.dto.CarStyleDto;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import com.woniu108.car.form.InsertCarStyleForm;
import com.woniu108.car.form.UpdateCarStyleForm;
import com.woniu108.car.model.CarStyle;
import com.woniu108.car.service.CarStyleService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆款式表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/carStyle")
@Api(tags = "汽车款式管理")
public class CarStyleController {
    @Resource
    private CarStyleService carStyleService;
    @Resource
    private RedisUtil redisUtil;
    /**
     *  查询所有未删除款式
     * @param seriesId 系列id
     * @return CarStyle集合
     */
    @GetMapping("/findAllCarStyle.api")
    @ApiOperation("查询所有未删除款式")
    @ApiImplicitParam(name = "seriesId",value = "系列id")
    public Result<List<CarStyleDto>> findAllCarStyle(Integer seriesId) {
        List<CarStyle> list = carStyleService.findAllCarStyle(seriesId);
        List<CarStyleDto> dtos = BeanUtil.copyToList(list, CarStyleDto.class);
        return Result.newListInstance(CarStyleDto.class).setData(dtos).setMsg("查询汽车所有未删除款式成功");
    }
    /**
     *  查询所有正常款式
     * @param seriesId 系列id
     * @return CarStyle集合
     */
    @GetMapping("findAllNormalCarStyle")
    @ApiOperation("查询所有正常款式")
    @ApiImplicitParam(name = "seriesId",value = "系列id")
    public Result<List<CarStyleDto>> findAllNormalCarStyle(Integer seriesId) {
        List<CarStyle> list = carStyleService.findAllNormalCarStyle(seriesId);
        List<CarStyleDto> dtos = BeanUtil.copyToList(list, CarStyleDto.class);
        return Result.newListInstance(CarStyleDto.class).setData(dtos).setMsg("查询汽车所有正常款式成功");
    }
    /**
     * 新增款式
     * @param form 系列id，款式名称，描述，价格，管理员id
     * @return boolean值
     */
    @PostMapping("/insertCarStyle.api")
    @ApiOperation("新增款式")@AdminHistoryLog
    public Result<Boolean> insertCarStyle(@RequestBody InsertCarStyleForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        InsertCarStyleParam param = BeanUtil.toBean(form, InsertCarStyleParam.class);
        param.setAdminId(adminId);
        Boolean bool = carStyleService.insertCarStyle(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("新增汽车品牌款式成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("新增汽车品牌款式失败").setData(false).setCode(500);
    }
    /**
     * 删除款式
     * @param styleIds 款式ID集合
     * @return boolean值
     */
    @PostMapping("/deleteCarStyle.api")
    @ApiOperation("删除款式")@AdminHistoryLog
    @ApiImplicitParam(name = "styleIds",value = "款式id集合")
    public Result<Boolean> deleteCarStyle(@RequestBody List<Integer> styleIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        DeleteCarStyleParam param = new DeleteCarStyleParam(styleIds, adminId);
        Boolean bool = carStyleService.deleteCarStyle(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("删除汽车品牌款式成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("删除汽车品牌款式失败").setData(false).setCode(500);
    }
    /**
     * 禁用款式
     * @param styleIds 款式ID集合
     * @return boolean值
     */
    @PostMapping("/banCarStyle.api")
    @ApiOperation("禁用汽车款式")@AdminHistoryLog
    @ApiImplicitParam(name = "styleIds",value = "款式id集合")
    public Result<Boolean> banCarStyle(@RequestBody List<Integer> styleIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        BanCarStyleParam param = new BanCarStyleParam(styleIds, adminId);
        Boolean bool = carStyleService.banCarStyle(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("禁用汽车品牌款式成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("禁用汽车品牌款式失败").setData(false).setCode(500);
    }
    /**
     * 解锁款式
     * @param styleIds 款式ID集合
     * @return boolean值
     */
    @PostMapping("/unlockCarStyle.api")
    @ApiOperation("解锁汽车款式")@AdminHistoryLog
    @ApiImplicitParam(name = "styleIds",value = "款式id集合")
    public Result<Boolean> unlockCarStyle(@RequestBody List<Integer> styleIds, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UnlockCarStyleParam param = new UnlockCarStyleParam(styleIds, adminId);
        Boolean bool = carStyleService.unlockCarStyle(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("解锁汽车品牌款式成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("解锁汽车品牌款式失败").setData(false).setCode(500);
    }
    /**
     * 解锁款式
     * @param form 款式名称 ，款式描述,车辆参考价
     * @return boolean值
     */
    @PostMapping("/updateCarStyle.api")
    @ApiOperation("修改汽车款式信息")@AdminHistoryLog
    public Result<Boolean> updateCarStyle(@RequestBody UpdateCarStyleForm form, @RequestHeader String token) {
        String refreshToken = redisUtil.getString(token);
        String adminId = JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        UpdateCarStyleParam param = BeanUtil.toBean(form, UpdateCarStyleParam.class);
        param.setAdminId(adminId);
        Boolean bool = carStyleService.updateCarStyle(param);
        if (bool) {
            return Result.getResult(Boolean.class).setMsg("修改汽车品牌款式成功").setData(true);
        }
        return Result.getResult(Boolean.class).setMsg("修改汽车品牌款式失败").setData(false).setCode(500);
    }
}

